﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Goals;
using PMPUtils.Form;
using PMPUtils.DB;
using PMPUtils.JIRA;
using System.Drawing;
using PMPUtils.Report;
using PMPUtils.Configuration;
using PMPUtils.Resources;

namespace PMP.Goals
{
    public partial class Dashboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            try
            {
                if (!IsPostBack)
                {
                    GoalsOperator.InitializeGoalsGridView(grvGoals);
                }

                String sprint = ddlSprints.SelectedValue.Trim();
                if (sprint.Equals(String.Empty)) { sprint = SprintOperator.ReadCurrentSprint(); }

                RefreshForm(sprint);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }
        }

        protected void grvGoals_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = grvGoals.Rows[index];

            String ID = GridViewOperator.GetCellStringValue(grvGoals, row, "ID");
            String taskID = GridViewOperator.GetCellStringValue(grvGoals, row, "Task");

            if (e.CommandName.Equals("EditGoal"))
            {
            }
            else if (e.CommandName.Equals("DeleteGoal"))
            {
            }
            else if (e.CommandName.Equals("ManageGoals"))
            {
                PopupForm manageGoals = new PopupForm(Response, "GoalManagement.aspx?GOAL=" + ID + "&TASK=" + taskID, "ViewGoalDetails", "225", "425", "250", "200");
                manageGoals.Popup();
            }
        }

        protected void grvGoals_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Tool tip for task
                int cellID = GridViewOperator.GetCellID(grvGoals, "Task");
                String taskID = e.Row.Cells[cellID].Text;

                if (taskID != null && !taskID.Equals(String.Empty) && !taskID.Equals("&nbsp;"))
                {
                    HyperLink task = new HyperLink();
                    task.Text = e.Row.Cells[cellID].Text;
                    task.NavigateUrl = "http://jira.redprairie.com/browse/" + e.Row.Cells[cellID].Text;
                    e.Row.Cells[cellID].Controls.Add(task);
                }
            }
        }

        protected void btnAddGoal_Click(object sender, EventArgs e)
        {
            PopupForm addGoal = new PopupForm(Response, "AddGoal.aspx","AddGoal", "1100", "500", "50", "100");
            addGoal.Popup();
        }

        protected void btnBackup_Click(object sender, EventArgs e)
        {
            try
            {
                String sprint = ddlSprints.SelectedValue.Trim();
                if (sprint.Equals(String.Empty)) { sprint = SprintOperator.ReadCurrentSprint(); }

                if (GoalsOperator.IsBackedUpAlready(sprint))
                {
                    Messenger.Show(Response, "Already backed up today.");
                    return;
                }

                GoalsOperator.Backup();
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
            }

            Messenger.Show(Response, "Backup Successfully.");
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                String sprint = ddlSprints.SelectedValue.Trim();
                if (sprint.Equals(String.Empty)) { sprint = SprintOperator.ReadCurrentSprint(); }

                RefreshForm(sprint);

                // Update the burndown by story number
                BurndownProgressOperator.UpdateBurndown(grvGoals, sprint);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
            }

            Messenger.Show(Response, "Refresh Successfully.");
        }

        private void RefreshForm(String sprint)
        {
            GoalsOperator.RefreshGoalsGridView(grvGoals, sprint);

            GridViewOperator.RefreshProgressBackgroundColor(grvGoals);
            GridViewOperator.RefreshJIRAStatusBackgroundColor(grvGoals);

            FormatGridView();
        }

        private void FormatGridView()
        {
            // Status
            foreach (GridViewRow row in grvGoals.Rows)
            {
                // Tool tip for task
                int jiraNumberCellID = GridViewOperator.GetCellID(grvGoals, "Task");
                String jiraNumber = GridViewOperator.GetCellStringValue(grvGoals, row, "Task");
                String goalID = GridViewOperator.GetCellStringValue(grvGoals, row, "ID");

                String description = GoalsOperator.ReadTips(goalID);
                row.Cells[jiraNumberCellID].ToolTip = description;

                // Variance
                int varianceCellID = GridViewOperator.GetCellID(grvGoals, "Variance");

                if (row.Cells[varianceCellID] == null || row.Cells[varianceCellID].Text.Equals(String.Empty) || row.Cells[varianceCellID].Text.Equals("&nbsp;"))
                {
                    continue;
                }

                double variance = Convert.ToDouble(row.Cells[varianceCellID].Text.Substring(0, row.Cells[varianceCellID].Text.Length - 1));

                if (variance >= 25)
                {
                    row.Cells[varianceCellID].BackColor = Color.Red;
                }
                else if (variance >= 0)
                {
                    row.Cells[varianceCellID].BackColor = Color.Yellow;
                }
            }
        }

        protected void btnSync_Click(object sender, EventArgs e)
        {
            try
            {
                String sprint = ddlSprints.SelectedValue.Trim();
                if (sprint.Equals(String.Empty)) { sprint = SprintOperator.ReadCurrentSprint(); }

                JIRAOperator.SyncCurrentSprint(sprint);

                RefreshForm(sprint);

                // Update the progress burndown based on story count
                BurndownProgressOperator.UpdateBurndown(grvGoals, sprint);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            Messenger.Show(Response, "Sync Successfully.");
        }

        protected void ddlSprints_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String sprint = ddlSprints.SelectedValue.Trim();

                RefreshForm(sprint);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
            }
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            PopupForm reportDashboard = new PopupForm(Response, "../report/Dashboard.aspx?SPRINT=" + ddlSprints.SelectedValue.Trim(), "ReportDashboard", "225", "350", "350", "200");
            reportDashboard.Popup();
        }

        protected void btnMaintenance_Click(object sender, EventArgs e)
        {
            PopupForm maintDashboard = new PopupForm(Response, "../Maintenance/Dashboard.aspx", "maintDashboard", "275", "350", "350", "200");
            maintDashboard.Popup();
        }

        protected void btnKnowledgeBase_Click(object sender, EventArgs e)
        {
            PopupForm knowledgeBase = new PopupForm(Response, "../Documentation/KnowledgeBase.aspx", "KnowledgeBase", "1150", "800", "50", "100");
            knowledgeBase.Popup();
        }

        protected void btnManagement_Click(object sender, EventArgs e)
        {
            PopupForm management = new PopupForm(Response, "../Management/Dashboard.aspx", "Management", "275", "350", "350", "200");
            management.Popup();
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
        }

        protected void btnCulture_Click(object sender, EventArgs e)
        {
            PopupForm culture = new PopupForm(Response, "../Documentation/CultureDashboard.aspx", "CultureDashboard", "225", "350", "250", "150");
            culture.Popup();
        }
    }
}
